// Copyright (c) 2021 Presto Labs Pte. Ltd.
// Author: leon

#pragma once

#include <memory>
#include <string>

#include "coin2/exchange/base/symbology/currency.h"
#include "coin2/exchange/base/symbology/util.h"

namespace coin2::exchange::phemex_futures::symbology {

class PhemexFuturesCurrency : public base::symbology::Currency,
                              public base::symbology::CurrencyFromStrMixin<PhemexFuturesCurrency> {
 public:
  using ExchangeType = base::market::ExchangeType;
  using MarketType = base::market::MarketType;
  using Currency = base::symbology::Currency;
  using base::symbology::Currency::native_currency;

 public:
  explicit PhemexFuturesCurrency(const std::string& currency)
      : Currency(currency, ToNativeCurrency(currency).value(), ExchangeType::Phemex) {}

  static std::unique_ptr<PhemexFuturesCurrency> FromNativeStrImpl(
      const std::string& native_currency_str,
      int64_t resolve_timestamp) {
    auto currency = FromNativeCurrency(native_currency_str, resolve_timestamp);
    if (!currency) {
      return {};
    }
    return std::make_unique<PhemexFuturesCurrency>(currency.value());
  }

  static std::unique_ptr<PhemexFuturesCurrency> FromNormStrImpl(
      const std::string& currency_str,
      int64_t resolve_timestamp) {
    auto native_currency = ToNativeCurrency(currency_str);
    if (!native_currency) {
      return {};
    }

    auto norm_currency = FromNativeCurrency(*native_currency, resolve_timestamp);
    if (!norm_currency) {
      return {};
    }
    return std::make_unique<PhemexFuturesCurrency>(norm_currency.value());
  }

 private:
  static std::optional<std::string> ToNativeCurrency(const std::string& currency_str) {
    std::string native_currency_str = StringToUpperCopy(currency_str);
    if (currency_str == "BCHN") {
      native_currency_str = "BCH";
    }

    //     static std::unordered_set<std::string> exception_native = {"U100SLP", "U1000000NFT",
    //     "U100000SHIB", "U100IOST", "U100JASMY", "U100PEOPLE", "U100VET"};
    //     if(exception_native.count(native_currency_str) > 0){
    //       native_currency_str[0] += 32; // Convert to lowercase letters
    //     }

    char c0 = native_currency_str[0];
    char c1 = native_currency_str[1];
    if (c0 == 'U' && (c1 >= '0' && c1 <= '9')) {
      native_currency_str[0] += 32;  // Convert to lowercase letters
    }

    return native_currency_str;
  }

  static std::optional<std::string> FromNativeCurrency(
      const std::string& native_currency_str,
      int64_t resolve_timestamp) {
    std::string currency_str = StringToUpperCopy(native_currency_str);
    if (native_currency_str == "BCH") {
      currency_str = "BCHN";
    }
    return currency_str;
  }
};

}  // namespace coin2::exchange::phemex_futures::symbology
